package com.task.sql.schedule.service.impl;/**
 * @ClassName : ExecutorCustomSQLServiceImpl
 * @Description :
 * @Author : liudonghai
 * @Date: 2024-09-13 13:41
 */

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;
import com.task.sql.schedule.mapper.ExecutorCustomSQLMapper;
import com.task.sql.schedule.service.ExecutorCustomSQLService;
import com.task.sql.schedule.util.db.SqlTypeSwitch;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : ExecutorCustomSQLServiceImpl
 * @Description :
 * @Author : liudonghai
 * @Date: 2024-09-13 13:41
 */
@Service
public class ExecutorCustomSQLServiceImpl implements ExecutorCustomSQLService {
    @Resource
    private ExecutorCustomSQLMapper executorCustomSQLMapper;

    @Override
    public List<Map<String, Object>> executorSelectSQL(String sql) {
        return executorCustomSQLMapper.selectSQL(sql);
    }

    @Override
    public int executorUpdateSQL(String sql) {
        return executorCustomSQLMapper.updateSQL(sql);
    }

    @Override
    public int executorDeleteSQL(String sql) {
        return executorCustomSQLMapper.deleteSQL(sql);
    }

    @Override
    public int executorInsertSQL(String sql) {
        return executorCustomSQLMapper.insertSQL(sql);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean executorSQLInTransaction(String sql) throws Exception {
        boolean res = Boolean.TRUE;
        if (sql.indexOf(";") < 0) {
            res = executorSQL(sql);
        } else if (sql.indexOf(";") == 0) {
            res = executorSQL(sql.substring(1, sql.length()));
        } else {
            String[] sqlArray = sql.split(";");
            for (String newSql : sqlArray) {
                boolean executorSQLRes = executorSQL(newSql);
                if (!executorSQLRes) {
                    throw new Exception("sql执行失败");
                }
            }
        }
        return res;
    }

    private boolean executorSQL(String sql) {
        int sqlType = SqlTypeSwitch.switchSqlType(sql);
        System.out.println(sql);
        boolean res = Boolean.TRUE;
        switch (sqlType) {
            //select
            case 0:
                List<Map<String, Object>> list = executorCustomSQLMapper.selectSQL(sql);
                if (CollectionUtil.isEmpty(list)) {
                    res = Boolean.FALSE;
                }
                break;
            //insert
            case 1:
                res = executorCustomSQLMapper.insertSQL(sql) > 0 ? Boolean.TRUE : Boolean.FALSE;
                break;
            //update
            case 2:
                res = executorCustomSQLMapper.updateSQL(sql) > 0 ? Boolean.TRUE : Boolean.FALSE;
                break;
            //delete
            case 3:
                res = executorCustomSQLMapper.deleteSQL(sql) > 0 ? Boolean.TRUE : Boolean.FALSE;
                break;
            default:
                List list1 = executorCustomSQLMapper.selectSQL(sql);
                if (CollectionUtil.isEmpty(list1)) {
                    res = Boolean.FALSE;
                }
                break;
        }
        return res;
    }

}
